package com.thundersoft.phonecontact;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.CallLog;
import android.util.Log;
import android.widget.Toast;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class CallItem implements Serializable {

    private static final String TAG = "CallItem";
    private static List<CallItem> listsAll = new ArrayList<>();
    private static List<CallItem> listsMissed = new ArrayList<>();
    private static List<CallItem> listsReceived = new ArrayList<>();

    private static List<List<CallItem>> lists = new ArrayList<>();

    public static final String CALL_TYPE_INCOMING = "incoming";
    public static final String CALL_TYPE_MISSED = "missed";
    public static final String CALL_TYPE_OUTGOING = "outgoing";
    public static final String CALL_TYPE_REJECTED = "rejected";

    private String callNumber;
    private String callType;
    private long callTime;

    private String cachedName;

    private CallItem(String callNumber, String callTpye, long callTime, String cachedName) {
        this.callNumber = callNumber;
        this.callType = callTpye;
        this.callTime = callTime;
        this.cachedName = cachedName;
    }

    public String getCallNumber() {
        return callNumber;
    }

    public String getCallType() {
        return callType;
    }

    public long getCallTime() {
        return callTime;
    }

    public String getCachedName() {
        return cachedName;
    }

    public static void updateList(ContentResolver cr) {
        listsAll.clear();
        listsMissed.clear();
        listsReceived.clear();
        lists.clear();
        Cursor cursor = null;
        String number;
        long callDateLong;
        String cachedName;
        String type;
        CallItem callItem;
        try {
            cursor = cr.query(CallLog.Calls.CONTENT_URI,
                    new String[]{CallLog.Calls.NUMBER, CallLog.Calls.TYPE, CallLog.Calls.DATE, CallLog.Calls.CACHED_NAME}
                    , null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                number = cursor.getString(0);
                callDateLong = Long.parseLong(cursor.getString(2));
                cachedName = cursor.getString(3);
                switch (cursor.getInt(1)) {
                    case CallLog.Calls.INCOMING_TYPE:
                        type = CALL_TYPE_INCOMING;
                        callItem = new CallItem(number, type, callDateLong, cachedName);
                        listsReceived.add(callItem);
                        break;
                    case CallLog.Calls.MISSED_TYPE:
                        type = CALL_TYPE_MISSED;
                        callItem = new CallItem(number, type, callDateLong, cachedName);
                        listsMissed.add(callItem);
                        break;
                    case CallLog.Calls.OUTGOING_TYPE:
                        type = CALL_TYPE_OUTGOING;
                        callItem = new CallItem(number, type, callDateLong, cachedName);
                        break;
                    default:
                        type = CALL_TYPE_REJECTED;
                        callItem = new CallItem(number, type, callDateLong, cachedName);
                        break;
                }
                listsAll.add(callItem);

            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (SecurityException se) {
//            se.printStackTrace();
            Log.e(TAG, "ERROR");
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        lists.add(listsAll);
        lists.add(listsMissed);
        lists.add(listsReceived);
    }

    public static List<CallItem> getLists(int position) {
        return lists.get(position);
    }


    public static void deleteItem(ContentResolver cr, long targetCallTime) {

        int time = cr.delete(CallLog.Calls.CONTENT_URI, "date=?", new String[]{targetCallTime + ""});
        Log.e(TAG, "deleteItem: \t" + time + "");
    }

    public static List<CallItem> deleteLog(ContentResolver cr) {
        cr.delete(CallLog.Calls.CONTENT_URI, null, null);
        return null;
    }

}
